package proxy

import (
	"net/http"

	"gitee.com/xuender/mysql-proxy/pb"
	"github.com/gin-gonic/gin"
	"github.com/xuender/oils/base"
)

type ConfigService struct {
	c *pb.Config
}

func NewConfigService(config *pb.Config) *ConfigService {
	return &ConfigService{c: config}
}

func (p *ConfigService) Group(group *gin.RouterGroup) {
	group.GET("", p.GET)
	group.POST("", p.POST)
}

func (p *ConfigService) GET(ctx *gin.Context) {
	ctx.JSON(http.StatusOK, p.c)
}

func (p *ConfigService) POST(ctx *gin.Context) {
	config := &pb.Config{}
	base.Panic(ctx.Bind(config))

	p.c.Port = config.Port
	p.c.GrpcPort = config.GrpcPort
	p.c.DefaultLimit = config.DefaultLimit
	p.c.WebPort = config.WebPort

	base.Panic(p.c.Save(""))
	ctx.JSON(http.StatusOK, p.c)
}
